#!/bin/bash

source /home/shell/util.sh

function install () {
  printInfo "Redis 版本列表："
  curl -sL https://repo.huaweicloud.com/redis/|grep -Po redis-.*.tar.gz|grep -v beta|uniq|perl -pe "s/redis-(.*?).tar.gz/\1/g"|tail -10
  while [ true ]; do
      read -p "选择版本：" version
      if [[ -z "${version}" ]]; then
          printFail "请重新输入版本！"
          continue;
      else
          break;
      fi
  done
  if [ ! -f "/opt/redis-stable.tar.gz" ];then
    download /opt/redis-stable.tar.gz https://repo.huaweicloud.com/redis/redis-${version}.tar.gz
  fi
  tar -xf /opt/redis-stable.tar.gz -C /opt
  rm -rf /opt/redis-stable.tar.gz
  mv /opt/redis-* /opt/redis-stable
  cd /opt/redis-stable
  # make distclean
  make && make install
}



if [ ! -f "/usr/local/bin/redis-server" ]; then
  install
fi

current_port=`cat /opt/redis-stable/redis.conf |grep -e "^port [0-9]*"|awk '{print $2}'`
read -p "请输入redis端口[当前端口-'$current_port']：" redis_port
read -p "请输入redis密码[redis123]：" redis_passwd
redis_passwd=${redis_passwd:-"redis123"};

if [ ! -d "/var/log/redis" ]; then
  mkdir -p /var/log/redis
fi

# redis基本配置
sed -i 's/daemonize no/daemonize yes/' /opt/redis-stable/redis.conf
sed -i 's/bind 127.0.0.1/bind 0.0.0.0/' /opt/redis-stable/redis.conf
sed -i 's/protected-mode yes/protected-mode no/' /opt/redis-stable/redis.conf
sed -i 's#logfile ""#logfile "/var/log/redis/redis.log"#' /opt/redis-stable/redis.conf|grep logfile
if [ -n "$redis_passwd" ]; then
  sed -i 's/# requirepass foobared/requirepass '$redis_passwd'/' /opt/redis-stable/redis.conf
fi

# 设置redis端口
if [ -n "$redis_port" ];then
 echo "变更redis端口为$redis_port"
 current_port=$redis_port
 sed -i 's/port '$current_port'/port '$redis_port'/' /opt/redis-stable/redis.conf
else
 echo "redis使用默认端口"$current_port
fi

firewall-addport $current_port

if [ -f /etc/rc.d/rc.local ];
then
  # 如果存在rc.local
  echo 'echo 511 > /proc/sys/net/core/somaxconn' >> /etc/rc.d/rc.local
  echo 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' >> /etc/rc.d/rc.local
fi

# 不存在rc.local
# sed -ri "/#bail if this system is managed by systemd/,+8d" /opt/redis-stable/utils/install_server.sh
# bash /opt/redis-stable/utils/install_server.sh

echo "net.core.somaxconn=4097" >> /etc/sysctl.conf
echo "vm.overcommit_memory=1" >> /etc/sysctl.conf
sysctl -p

if [ $(ostype) == "Ubuntu" ];
then
  echo "执行。。。"
  echo "kernel/mm/transparent_hugepage/enabled=never">>/etc/sysfs.conf
  echo "kernel/mm/transparent_hugepage/defrag=never">>/etc/sysfs.conf
  /etc/init.d/sysfsutils restart
fi

printInfo "系统参数变更："
sysctl -a|grep somaxconn
sysctl -a|grep overcommit_memory
ulimit -n


bash /home/shell/serviced-install.sh<<EOF
2
1

redis
redis-server /opt/redis-stable/redis.conf


redis-cli -p $current_port -a $redis_passwd shutdown

EOF

# sed -i "s/\$CLIEXEC -p \$REDISPORT shutdown/\$CLIEXEC -p \$REDISPORT -a "$redis_passwd" shutdown/g" /etc/init.d/redis_$current_port
# systemctl daemon-reload
# /lib/systemd/systemd-sysv-install enable redis_6379
# systemctl start redis_$current_port
# systemctl status redis_$current_port.service

while [ true ]; do
  if [ -f "/var/log/redis/redis.log" ]; then
    tail -200 /var/log/redis/redis.log
    break;
  fi
  sleep 1s
  loadMsg "redis启动中..."
done




printInfo "====================redis===================="
printInfo "地址：$host_addr"
printInfo "端口：$current_port"
printInfo "密码：$redis_passwd"
printInfo "日志：/var/log/redis/redis.log"
printInfo "====================redis===================="


while [ true ]; do
  read -p "redis安装完成，需要重启系统，是否重启？[Y/N]" is_boot_restart
  case "${is_boot_restart}" in
    y|Y)
      echo "系统重启中..."
      reboot
      break;
    ;;
    n|N)
      echo "请稍后手动重启以便设置生效。"
      break;
    ;;
    *)
      echo "输入有误，重新输入……"
    ;;
  esac
  
done